package com.cskaoyan.service.vx;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.cskaoyan.bean.bo.vxBo.PasswordResetBo;
import com.cskaoyan.bean.bo.vxBo.VxRegisterBo;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.mapper.wxMapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.wxMapper.MarketUserMapper;
import com.cskaoyan.util.CodeUtils;
import com.cskaoyan.util.EncryptUtils;
import com.cskaoyan.util.Sample;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: DYQ
 * @time: Created on 2022/6/8 14:08
 */

@Service
public class AuthServiceImpl implements AuthService{

    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public String sendMsg(String mobile) throws Exception {


        //TODO 这里以后随机生成一个四位数的字符串
        String code = "{\"code\":\"";

        String msg = CodeUtils.getRandomString(4);
        String suf = "\"}";

        System.out.println(code + msg + suf);


        com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI5tPis2xPGpBpBCro5bnc", "oYNXafzCJi4RUJJQoELUKXm13tXzRR");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(mobile)
                .setTemplateParam(code + msg +suf);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return msg;


    }


    @Override
    public List<String> getUserName() {
        List<String> userNames = marketUserMapper.selectAllUserName();
        return userNames;
    }

    @Override
    public void addUser(VxRegisterBo registerBo, HttpServletRequest request) {
        String encryptPassword = EncryptUtils.encrypt(registerBo.getPassword());
        String addr = request.getRemoteAddr();
        MarketUser marketUser = new MarketUser();
        Date date = new Date();
        marketUser.setId(null);
        marketUser.setUsername(registerBo.getUsername());
        marketUser.setPassword(encryptPassword);
        marketUser.setWeixinOpenid(registerBo.getWxCode());
        marketUser.setDeleted(false);
        marketUser.setAddTime(date);
        marketUser.setUpdateTime(date);
        marketUser.setMobile(registerBo.getMobile());
        marketUser.setUserLevel((byte) 0);
        marketUser.setStatus((byte) 0);
        marketUser.setLastLoginIp(addr);
        marketUser.setNickname(registerBo.getUsername());
        marketUser.setAvatar("http://localhost:8083/pic/80fed7d04cb14c0788354d1c49ed9e77.jpg");
        int i = marketUserMapper.insertSelective(marketUser);
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(marketUser.getId());
        marketCouponUser.setCouponId(3);
        marketCouponUser.setStatus((short)0);
        Date startTime = new Date();
        marketCouponUser.setStartTime(startTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DATE,7);
        Date endTime = calendar.getTime();
        marketCouponUser.setEndTime(endTime);
        marketCouponUser.setAddTime(startTime);
        marketCouponUser.setDeleted(false);
        marketCouponUserMapper.insertSelective(marketCouponUser);
    }

    @Override
    public List<String> getMobile() {
        List<String> mobiles =  marketUserMapper.selectAllMobiles();
        return mobiles;
    }

    @Override
    public void resetPassword(PasswordResetBo passwordResetBo) {
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        if (passwordResetBo.getMobile() != null && passwordResetBo.getMobile() != "") {
            criteria.andMobileEqualTo(passwordResetBo.getMobile());
        }
        MarketUser marketUser = new MarketUser();
        marketUser.setPassword(passwordResetBo.getPassword());
        marketUserMapper.updateByExampleSelective(marketUser, example);
    }

}
